package obecto.task.container
{
	import obecto.task.IInterruptibleTask;
	import obecto.task.IPausableTask;
	
	public class PausableParallel extends InterruptibleParallel implements IPausableTask
	{
		private var _paused:Boolean;
		
		public function PausableParallel()
		{
			super();
		}
		
		public function get paused():Boolean
		{
			return _paused;
		}
		
		public function set paused(value:Boolean):void
		{
			if (_paused != value)
			{
				_paused = value;
				
				if(_paused)
				{
					pause();
				}
				else
				{
					resume();
				}
			}
		}
		
		protected function pause():void
		{
			for each (var task:IInterruptibleTask in tasks)
			{
				if (task is IPausableTask)
				{
					IPausableTask(task).paused = true;
				}
				else
				{
					task.interrupt();
				}
			}
		}
		
		protected function resume():void
		{
			for each (var task:IInterruptibleTask in tasks)
			{
				if (task is IPausableTask)
				{
					IPausableTask(task).paused = false;
				}
				else
				{
					task.start();
				}
			}
		}
	}
}